export const ObjectToArray = <T>(
  value: Record<string, any> | null
): T[] | null => (value ? Object.values(value) : null);
export const ObjectToKeyArray = <T>(
  value: Record<string, any> | null
): { key: string; value: T }[] | null =>
  value
    ? Object.keys(value).map((e) => {
        return { value: value[e], key: e };
      })
    : null;

export const MapToArray = <T>(value: Map<any, any> | null) => {
  if (!value || !(value instanceof Map)) {
    return [];
  }
  return Array.from(value.values()) as T[];
};
export const MapToKeyArray = <T>(value: Map<any, any> | null) => {
  if (!value || !(value instanceof Map)) {
    return [];
  }
  return Array.from(value) as T[];
};
export const ArrayToKeyMap = <T>(
  value: Array<any> | null,
  key: string
): Map<string, T> => {
  if (!value || !(value instanceof Array)) {
    return new Map();
  }
  const map = new Map();
  value.forEach((item) => map.set(item[key], item));
  return map;
};

export const IterationObject = <T>(
  value: Object,
  callback: (key: string, value: T) => void
) => {
  if (!value) {
    return;
  }
  Object.entries(value).forEach(([key, value]) => callback(key, value));
};

export const concatenateKeyValue = (obj: Record<string, any>): string => {
  return Object.entries(obj)
    .map(([key, value]) => (value ? `${key} ${value}` : ""))
    .join(" ");
};
